<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/15
 * Time: 17:38
 */

namespace App\Rep\Rbac;


use App\Models\Rbac\Node;
use App\Models\Rbac\Role;
use App\Models\Rbac\RoleNode;
use App\Models\Rbac\User;
use App\Models\Rbac\UserRole;
use Illuminate\Support\Facades\DB;

class RoleRep
{
    //获取角色列表
    public function roleListRep($params){
        return Role::Search($params)->with(['user'])->paginate($params['per_page']??1000);
    }

    //添加角色
    public function createRoleRep($params){
        if(isset($params['id']) && !empty($params['id'])){
            $model = Role::where('id',$params['id'])->first();
        }else{
            $model = Role::where('name',$params['name'])->first();
            if(!$model){
                $model = new Role();
            }
        }
        $model->name = $params['name'];
        $model->save();
        //RoleNode::updateOrCreate(['role_id'=>$model->id,'node_id'=>7],['role_id'=>$model->id,'node_id'=>7]);

        return $model;
    }

    //删除角色
    public function delRoleRep($params){
        $model = Role::find($params['id']);
        $model->is_del = 1;
        $model->save();
        UserRole::where('role_id',$model->id)->delete();
        RoleNode::where('role_id',$model->id)->delete();
        return $model;
    }

    public function setRoleStatusRep($params){
        $model = Role::find($params['id']);
        $model->status = $params['status'];
        $model->save();
        return $model;
    }

    //管理员分配权限
    public function allocationNodeRep($params){

        DB::beginTransaction();
        try {
            $last_nodeid_arr = RoleNode::where('role_id',$params['roles_id'])->pluck('node_id')->toArray();
            $new_nodeid_arr = $params['node_id'];
            $diff_arr1 = array_diff($last_nodeid_arr,$new_nodeid_arr);
            $diff_arr2 = array_diff($new_nodeid_arr,$last_nodeid_arr);
            if(!empty($diff_arr1)){
                foreach($diff_arr1 as $v){
                    RoleNode::where('role_id',$params['roles_id'])->where('node_id',$v)->delete();
                }
            }
            if(!empty($diff_arr2)){
                foreach($diff_arr2 as $v){
                    RoleNode::insert(['role_id'=>$params['roles_id'],'node_id'=>$v]);
                }
            }
            DB::commit();
            return ['code'=>0,'data'=>'','msg'=>'success'];
        }catch (\Exception  $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>$e->getMessage(),'msg'=>'error'];
        }
    }
    //获取角色已分配权限列表
    public function getroleNodeListRep($role_id){
        $c_id_arr = RoleNode::where('role_id',$role_id)->pluck('node_id')->toArray();
        //p($c_id_arr);
        // $all = Node::where('is_del',2)
        //     ->select('id', 'name as title','route','description','created_at','updated_at','pid','icon','is_menu','path','sort','is_del','component','redirect')
        //     ->get()->toArray();
        // foreach($all as $k=>$v){
        //     $all[$k]['is_check'] = 2;
        //     if(in_array($v['id'],$c_id_arr)){
        //         $all[$k]['is_check'] = 1;
        //     }
        // }
        // $list = subTree($all);
        return $c_id_arr;
    }

    /**
     * 获取角色已分配权限
     * @param $role_id
     * @return array
     */
    public function getRoleNodeInfoRep($role_id){
        $list = RoleNode::where('role_id',$role_id)->with('node')->get()->toArray();
        return $list;
    }

    /**
     * 获取角色为【业务员】的账号列表
     */
    public function getYeWuYuanListRep($params){
        $role_id = 3;
        $role = Role::where('id',$role_id)->with(['user'=>function($q)use($params){
            if(!empty($params['company_name'])){
                $q->where('company_name','like','%'.$params['company_name'].'%');

            }
        }])->first();
        return $role->user;
    }

    /**
     * 获取角色为【员工】的账号列表
     * @return mixed
     */
    public function getYuanGongListRep(){
        $role_id = 16;
        $role = Role::where('id',$role_id)->with('user')->first();
        return $role->user;
    }

    /**
     * 获取角色为【送货员】的账号列表
     * @return mixed
     */
    public function getSonghuoyuanListRep(){
        $role_id = 19;
        $user_id_arr = UserRole::where('role_id',$role_id)->pluck('user_id')->toArray();
        $list = User::whereIn('id',$user_id_arr)->get();
        return $list;
    }


    /**
     * 获取当前角色按钮的权限
     * @param $role_id
     */
    public function getButtonRoute($role_id){

        $role = Role::where('id',$role_id)->with('button')->first()->toArray();
        $permissions = [];
        if($role_id == 1){
            $permissions = ["*:*:*"];
        }else{
            if(!empty($role['button'])){
                foreach($role['button'] as $k=>$v){
                    $permissions[] = $v['route'];
                }
            }
        }
        return $permissions;
    }


}
